package com.gomoku

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext

// 对手列表窗口
class RivalWindow {
    private var mData: List<String>                     // 数据，即所有用户名
    private lateinit var mShow: MutableState<Boolean>   // 是否显示
    private var mOnCancel: () -> Unit = {}              // 用户单击"取消"按钮事件
    private var mOnOK: (String) -> Unit = {}            // 用户单击"确定"按钮事件

    /**
     * 构造方法。
     */
    constructor() {
        mData = listOf()
    }

    /**
     * 界面布局。
     */
    @Composable
    fun Greeting() {
        mShow = remember {
            mutableStateOf(false)
        }
        var selectedItemIndex by remember {
            mutableIntStateOf(-1)
        }
        if (mShow.value) {
            val context = LocalContext.current
            AlertDialog(
                onDismissRequest = {},
                title = {
                    Text(context.getString(R.string.IDS_RIVAL_LIST))
                },
                text = {
                    LazyColumn(Modifier.fillMaxWidth()) {
                        itemsIndexed(mData) { index, item ->
                            Text(
                                item,
                                Modifier
                                    .fillMaxWidth()
                                    .clickable {
                                        selectedItemIndex = index
                                    }
                                    .padding(top = Const.RIVAL_WINDOW_ITEM_PADDING, bottom = Const.RIVAL_WINDOW_ITEM_PADDING)
                                    .background(if (index == selectedItemIndex) Color.LightGray else Color.Transparent)
                            )
                            HorizontalDivider(thickness = Const.RIVAL_WINDOW_DIVIDER_HEIGHT)
                        }
                    }
                },
                confirmButton = {
                    TextButton({
                        if (selectedItemIndex >= 0) {
                            this.mOnOK(mData[selectedItemIndex])
                        }

                        // 关闭窗口
                        mShow.value = false
                    }) {
                        Text(context.getString(android.R.string.ok))
                    }
                },
                dismissButton = {
                    TextButton({
                        this.mOnCancel()

                        // 关闭窗口
                        mShow.value = false
                    }) {
                        Text(context.getString(android.R.string.cancel))
                    }
                }
            )
        }
    }

    /**
     * 隐藏。
     */
    fun hide() {
        mShow.value = false
    }

    /**
     * 设置用户名列表。
     * @param usernameList  用户名列表
     */
    fun setUsernameList(usernameList: List<String>) {
        mData = usernameList
    }

    /**
     * 显示。
     * @param onCancel  用户单击"取消"按钮事件
     * @param onOK      用户单击"确定"按钮事件
     */
    fun show(onCancel: () -> Unit = {}, onOK: (String) -> Unit = {}) {
        mOnCancel = onCancel
        mOnOK = onOK
        mShow.value = true
    }
}
